<!DOCTYPE html>
<html>
<head>
    <title>设备信息</title>
    {% include 'system/common/header.html' %}
    <link rel="stylesheet" href="{{ url_for('static', filename='system/admin/css/other/user.css') }}"/>
</head>
<body class="pear-container">
{# 查询表单 #}
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="" lay-filter="device-query-form">
            <div class="layui-form-item">
                <label class="layui-form-label">设备名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="deviceName" placeholder="" class="layui-input">
                </div>
                <label class="layui-form-label">生产公司</label>
                <div class="layui-input-inline">
                    <input type="text" name="producer" placeholder="" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="device-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="user-left user-collasped">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="button button-primary user-group" user-group=""> 全 部 用 户</div>
            <div class="button button-default user-group" user-group="-1"> 默 认 分 组</div>
            <div style="overflow: auto">
                <ul id="dept-tree" class="dept-tree" data-id="0"></ul>
            </div>
        </div>
    </div>
</div>
{# 设备表格 #}
<div class="user-main user-collasped">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="device-table" lay-filter="device-table"></table>
        </div>
    </div>
</div>
</body>
{# 表格操作 #}
<script type="text/html" id="device-toolbar">
    {% if authorize("system:device:add") %}
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
            <i class="pear-icon pear-icon-add"></i>
            新增
        </button>
    {% endif %}
</script>

{# 用户修改操作 #}
<script type="text/html" id="device-bar">
    {% if authorize("system:device:borrow") %}
        <button class="pear-btn layui-bg-purple pear-btn-sm" lay-event="borrow"><i
                class="layui-icon layui-icon-find-fill">
            借出</i>
        </button>
    {% endif %}
    {% if authorize("system:device:edit") and authorize("system:device:remove") %}
        <a class="layui-btn layui-btn-sm" lay-event="more">
            更多
            <i class="layui-icon layui-icon-down"></i>
        </a>
    {% endif %}
</script>

{% raw %}
<script type="text/html" id="device-enable">
    <input type="checkbox" name="enable" value="{{ d.id }}" lay-skin="switch" lay-text="可用|报废"
           lay-filter="device-enable"
           {{# if(d.enable==1){ }} checked {{# } }}/>
</script>

{% endraw %}

{% include 'system/common/footer.html' %}

<script>
    function render_status(d) {
        if (d.status === 1) {
            return '<span class="layui-badge layui-bg-green">在库</span>';
        } else if (d.status === 0) {
            return '<span class="layui-badge layui-bg-gray">外借</span>';
        }
    }

    function render_PurchaseTime(d) {
        return layui.util.toDateString(d.purchase_at)
    }


    layui.use(['table', 'dtree', 'form', 'jquery', 'popup', 'common'], function () {
        let table = layui.table
        let form = layui.form
        let $ = layui.jquery
        let popup = layui.popup
        let dropdown = layui.dropdown
        let MODULE_PATH = "{{ url_for('system.device.main') }}"
        // 表格数据
        let cols = [
            [
                {title: 'id', field: 'id', align: 'center', width: 20},
                {title: '设备名称', field: 'deviceName', align: 'center', width: 110},
                {title: '设备型号', field: 'typeNum', align: 'center'},
                {title: '生产厂家', field: 'producer', align: 'center'},
                {title: '购买时间', field: 'purchase_at', templet: render_PurchaseTime, align: 'center'},
                {title: '保修信息', field: 'warranty', align: 'center', width: 80},
                {title: '是否可用', field: 'enable', align: 'center', templet: '#device-enable', width: 120},
                {title: '状态', field: 'status', align: 'center', templet: render_status, width: 120},
                {title: '操作', toolbar: '#device-bar', align: 'center', width: 180}
            ]
        ]

        // 渲染表格数据
        table.render({
            elem: '#device-table',
            url: MODULE_PATH + 'data',
            page: true,
            cols: cols,
            skin: 'line',
            height: 'full-148',
            toolbar: '#device-toolbar', /*工具栏*/
            text: {none: '暂无相关信息'},
            defaultToolbar: [{layEvent: 'refresh', icon: 'layui-icon-refresh'}, 'filter', 'print', 'exports'] /*默认工具栏*/
        })


        table.on('tool(device-table)', function (obj) {
            if (obj.event === 'borrow') {
                window.borrow(obj)
            } else if (obj.event === 'more') {
                dropdown.render({
                    elem: this,
                    show: true,
                    data: [{
                        id: 'edit',
                        title: '编辑',
                    }, {
                        id: 'delete',
                        title: '删除',
                    }],
                    click: function (data) {
                        if (data.id === 'edit') {
                            window.edit(obj)
                        } else if (data.id === 'delete') {
                            window.remove(obj)
                        }
                    },
                    align: 'right',
                })
            }
        })

        table.on('toolbar(device-table)', function (obj) {
            if (obj.event === 'add') {
                window.add()
            } else if (obj.event === 'refresh') {
                window.refresh()
            }
        })

        form.on('submit(device-query)', function (data) {
            window.refresh(data.field)
            return false
        })

        form.on('switch(device-enable)', function (obj) {
            let operate
            if (obj.elem.checked) {
                operate = 'enable'
            } else {
                operate = 'disable'
            }
            let loading = layer.load()
            $.ajax({
                url: '{{ url_for('system.device.main') }}' + operate,
                data: JSON.stringify({deviceId: this.value}),
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
                success: function (result) {
                    layer.close(loading)
                    if (result.success) {
                        popup.success(result.msg)
                    } else {
                        popup.failure(result.msg)
                    }
                }
            })
        })

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['550px', '550px'],
                content: MODULE_PATH + 'add',
                end: function () {
                    window.refresh()
                }
            })
        }

        window.borrow = function (obj) {
            layer.alert('借用设备说明：<br>1. 免费借用2天，往后1元/天，请及时归还设备<br>2. 设备归还后，请及时将设备归还给管理员<br>3. 请爱惜设备，祝您使用愉快', {
                icon: 3,
                title: '借出设备'
            }, function (index) {
                layer.close(index)
                let loading = layer.load()
                $.ajax({
                    url: MODULE_PATH + 'borrow/' + obj.data['id'],
                    dataType: 'json',
                    type: 'put',
                    success: function (result) {
                        layer.close(loading)
                        if (result.success) {
                            popup.success(result.msg)
                            window.refresh()
                        } else {
                            popup.failure(result.msg)
                        }
                    }
                })
            })
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['550px', '500px'],
                content: MODULE_PATH + 'edit/' + obj.data['id'],
                end: function () {
                    window.refresh()
                }
            })
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该设备', {icon: 3, title: '提示'}, function (index) {
                layer.close(index)
                let loading = layer.load()
                $.ajax({
                    url: MODULE_PATH + 'remove/' + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading)
                        if (result.success) {
                            popup.success(result.msg, function () {
                                obj.del()
                            })
                        } else {
                            popup.failure(result.msg)
                        }
                    }
                })
            })
        }


        window.refresh = function (param) {
            table.reload('device-table', {where: param})
        }
    })
</script>
</html>